<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\Information;
use App\Models\Node;
use App\Models\Post;
use App\Models\Product;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class TestController extends Controller
{
    public function index(Request $request)
    {
        return response()->json(['code' => 200]);
    }

    // public function index(Request $request)
    // {
    //     $pageSize = $request->pageSize ?: 50;
    //     $page = $request->page ?: 1;
    //     $skip = $page * $pageSize - $pageSize;
    //     $course = Course::where('id','>',37)->whereNotNull('tag')->skip($skip)->take($pageSize)->get();
    //     foreach ($course as $key => $v) {
    //         $data = $this->res($v->tag);
    //         // $content = $data['org_content'];
    //         $v->content = $data['org_content'];
    //         $v->cover = $this->uploadImg($data['img_url']);
    //         $v->brief = $data['summary'];
    //         $v->save();
    //     }

    //     return response()->json(['code' => 200,'course' => $course]);
    // }

    public function res($resource_id)
    {
        $response = Http::withHeaders([
            'Cookie' => 'XIAOEID=1436c559137476c041cde34a05417497; channel=admin; cookie_channel=admin; cookie_referer=; cookie_session_id=8501337bf7f047b762d4b2e626a38f21; new_template=KUh297Bg5fp24T8; mobile_manage=0; sajssdk_2015_cross_new_user=1; b_user_token=token_6926698fdda7dZ7alkWirn5HLzQLlMmdz; report_point_flag=b_u_5ed2028a6952e_kJ85Q7J2; xiaoe_admin_is_login=1; cookie_is_signed=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22b_u_5ed2028a6952e_kJ85Q7J2%22%2C%22first_id%22%3A%2219abe0bea4f560-0673b931ae3bdf-1d525631-1764000-19abe0bea502b6e%22%2C%22props%22%3A%7B%7D%2C%22%24device_id%22%3A%2219abe0bea4f560-0673b931ae3bdf-1d525631-1764000-19abe0bea502b6e%22%7D; management_version=2; is_new_management=1; appsc=appqUtxrdB62642; with_app_id=appqUtxrdB62642; shop_type=lMmdz; b_logintime=1764125089',
        ])->get('https://admin.xiaoe-tech.com/xe.course.b_admin_r.resource.detail.get/1.0.0?resource_id='.$resource_id);

        // 处理响应
        $body = $response->getBody();
        $data = json_decode($body, true);
        $data = $data['data'];
        return $data;
    }

    public function uploadImg($source){
        $secretId ='AKID0sbCcVFrMk294rTwELFyVGqDh5cw40pT'; //替换为用户的 secretId，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
        $secretKey = 'Xr4A9BYZaR6bu780WwFammiVA5qRuEvU'; //替换为用户的 secretKey，请登录访问管理控制台进行查看和管理，https://console.cloud.tencent.com/cam/capi
        $region = "ap-shanghai"; //替换为用户的 region，已创建桶归属的 region 可以在控制台查看，https://console.cloud.tencent.com/cos5/bucket
        $cosClient = new \Qcloud\Cos\Client(
            array( 'region' => $region, 'schema' => 'https', 'credentials'=> array( 'secretId'  => $secretId , 'secretKey' => $secretKey))
        );

        $v_ = explode('?',$source);
        $extension = pathinfo($v_[0], PATHINFO_EXTENSION);
        $imageContent = file_get_contents($source);
        if ($imageContent) {
            $filename = uniqid().'-'.time() . '.'.$extension; // 生成文件名
            try {
                $bucket = "xinzhu-1301398555";
                $key = 'classroom/course/'.$filename; //此处的 key 为对象键，对象键是对象在存储桶中的唯一标识
                $file = $imageContent;
                if ($file) {
                    $result = $cosClient->putObject(array('Bucket' => $bucket,'Key' => $key,'Body' => $file));
                    return 'https://'.$result['Location'];
                }
            } catch (\Exception $e) {
                echo "$e\n";
            }
        }else{
            return response()->json(['code' => '403', 'msg' => '无法访问或获取图像。']);
        }
    }

    // public function index(Request $request)
    // {
    //     // 验证请求
    //     // 带请求头的请求
    //     $response = Http::withHeaders([
    //         'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiY2M1OGQzMzQ0YTUwZTU0ODE4ODA1MDcwYmJlMThmZjlhMThhY2M4MDgxYmM4OTc5NDJlNTUwZmNlNThjN2E2ZWI2OWUwNGI0OTI0MGVlNjciLCJpYXQiOjE3NTI4MjE3ODcuMjIyMTMsIm5iZiI6MTc1MjgyMTc4Ny4yMjIxMzksImV4cCI6MTc4NDM1Nzc4Ny4yMDkwODcsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.cse_j0C7dFqtpMa-Dcr2Oj8Z83kLskVigtJm7Dbj1yw9sXcf3Csb4xe9z2FVUprniXoACCtmZ0YhkkzGGJ9DJuljfrvsi2PaSk00ZWvx7VZCWmZUtmyXiIb1JdzLJ9yNK3ktQufJqdjNwDXsj6pmVYtWqfU3cxdZ5OgGPRdFtVzMJPj05HBwGHG-BYWyg0sepdavvGlx5eK5T-cgmtkbDMkcxD3xOkBVl4vvrCXNiEH3EyrwBvjGos9kM3jA876u2lUIQMcqPtsIRAZVrM755HPT-xSW9iKMR4rWZs5EQ8Wr7rYDAxqJx0QHXoR3GWUkiVE9mHjH-PXypsuKl9CNDL8QGxFKJIzTszJ6TsPpKllYocyqT4aahgBychjjzHSi2ilI_Fjs_HYYuQYh9cz_HqRjUkwWexxJDI1cWiw3SHa5GWfylEKUjTE8XQL028pTgcmJc6MrTaMxEffG33hyMqEO0ITgu7rGWsi3VdZGBo5zxF1VqnIxxA0BSws57QUZVPsrZVO4rKD23PsahSl6URZFczI2ngT-lxfGTCkyCnNGdCVDvizbjDXzlT-7l5lEVq8CLRDA_XRb_d66v_YiQdR_IpLz8iANyg85uHuFeg05zq_w4XbsDY-Ono_WNhLuq_BYp0qHx0DydLcivpSzY1u2umCeR7JBVwYcczUXJLg',
    //         'Content-Type' => 'application/json'
    //     ])->post('https://adminapi.xinzhuvip.com/api/applet/information', [
    //         'page' => 1,
    //         'limit' => '300'
    //     ]);

    //     // 处理响应
    //     $body = $response->getBody();
    //     $data = json_decode($body, true);
    //     $data = $data['data'];
    //     $insert = [];
    //     foreach ($data as $k => $v) {
    //         $arr = [];
    //         $arr['title'] = $v['title'];
    //         $arr['cover'] = $v['img'];
    //         $arr['type_id'] = $v['type'];
    //         $arr['pan'] = $v['pan'];
    //         $arr['price'] = $v['price'];
    //         $arr['points'] = $v['integral'];
    //         $arr['clicks'] = $v['click'];
    //         $arr['content'] = $v['content'];
    //         $insert[] = $arr;
    //     }
    //     Information::insert($insert);
    //     return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    // }

    // public function index(Request $request)
    // {
    //     // 验证请求
    //     // 带请求头的请求
    //     $response = Http::withHeaders([
    //         'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiY2M1OGQzMzQ0YTUwZTU0ODE4ODA1MDcwYmJlMThmZjlhMThhY2M4MDgxYmM4OTc5NDJlNTUwZmNlNThjN2E2ZWI2OWUwNGI0OTI0MGVlNjciLCJpYXQiOjE3NTI4MjE3ODcuMjIyMTMsIm5iZiI6MTc1MjgyMTc4Ny4yMjIxMzksImV4cCI6MTc4NDM1Nzc4Ny4yMDkwODcsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.cse_j0C7dFqtpMa-Dcr2Oj8Z83kLskVigtJm7Dbj1yw9sXcf3Csb4xe9z2FVUprniXoACCtmZ0YhkkzGGJ9DJuljfrvsi2PaSk00ZWvx7VZCWmZUtmyXiIb1JdzLJ9yNK3ktQufJqdjNwDXsj6pmVYtWqfU3cxdZ5OgGPRdFtVzMJPj05HBwGHG-BYWyg0sepdavvGlx5eK5T-cgmtkbDMkcxD3xOkBVl4vvrCXNiEH3EyrwBvjGos9kM3jA876u2lUIQMcqPtsIRAZVrM755HPT-xSW9iKMR4rWZs5EQ8Wr7rYDAxqJx0QHXoR3GWUkiVE9mHjH-PXypsuKl9CNDL8QGxFKJIzTszJ6TsPpKllYocyqT4aahgBychjjzHSi2ilI_Fjs_HYYuQYh9cz_HqRjUkwWexxJDI1cWiw3SHa5GWfylEKUjTE8XQL028pTgcmJc6MrTaMxEffG33hyMqEO0ITgu7rGWsi3VdZGBo5zxF1VqnIxxA0BSws57QUZVPsrZVO4rKD23PsahSl6URZFczI2ngT-lxfGTCkyCnNGdCVDvizbjDXzlT-7l5lEVq8CLRDA_XRb_d66v_YiQdR_IpLz8iANyg85uHuFeg05zq_w4XbsDY-Ono_WNhLuq_BYp0qHx0DydLcivpSzY1u2umCeR7JBVwYcczUXJLg',
    //         'Content-Type' => 'application/json'
    //     ])->post('https://adminapi.xinzhuvip.com/api/PersonalWx', [
    //         'page' => 1,
    //         'limit' => '300'
    //     ]);

    //     // 处理响应
    //     $body = $response->getBody();
    //     $data = json_decode($body, true);
    //     $data = $data['data'];
    //     $insert = [];
    //     foreach ($data as $k => $v) {
    //         $arr = [];
    //         $arr['name'] = $v['name'];
    //         $arr['avatar'] = $v['head_img'];
    //         $arr['mobile'] = $v['tell'];
    //         $arr['openid'] = $v['openid'];
    //         $arr['wx_unionid'] = $v['wx_unionid'];
    //         $arr['is_join'] = $v['is_join'];
    //         $arr['fans'] = $v['fans'];
    //         $insert[] = $arr;
    //     }
    //     User::insert($insert);
    //     return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    // }

    // public function index(Request $request)
    // {
    //     // 验证请求
    //     // 带请求头的请求
    //     $response = Http::withHeaders([
    //         'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiY2M1OGQzMzQ0YTUwZTU0ODE4ODA1MDcwYmJlMThmZjlhMThhY2M4MDgxYmM4OTc5NDJlNTUwZmNlNThjN2E2ZWI2OWUwNGI0OTI0MGVlNjciLCJpYXQiOjE3NTI4MjE3ODcuMjIyMTMsIm5iZiI6MTc1MjgyMTc4Ny4yMjIxMzksImV4cCI6MTc4NDM1Nzc4Ny4yMDkwODcsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.cse_j0C7dFqtpMa-Dcr2Oj8Z83kLskVigtJm7Dbj1yw9sXcf3Csb4xe9z2FVUprniXoACCtmZ0YhkkzGGJ9DJuljfrvsi2PaSk00ZWvx7VZCWmZUtmyXiIb1JdzLJ9yNK3ktQufJqdjNwDXsj6pmVYtWqfU3cxdZ5OgGPRdFtVzMJPj05HBwGHG-BYWyg0sepdavvGlx5eK5T-cgmtkbDMkcxD3xOkBVl4vvrCXNiEH3EyrwBvjGos9kM3jA876u2lUIQMcqPtsIRAZVrM755HPT-xSW9iKMR4rWZs5EQ8Wr7rYDAxqJx0QHXoR3GWUkiVE9mHjH-PXypsuKl9CNDL8QGxFKJIzTszJ6TsPpKllYocyqT4aahgBychjjzHSi2ilI_Fjs_HYYuQYh9cz_HqRjUkwWexxJDI1cWiw3SHa5GWfylEKUjTE8XQL028pTgcmJc6MrTaMxEffG33hyMqEO0ITgu7rGWsi3VdZGBo5zxF1VqnIxxA0BSws57QUZVPsrZVO4rKD23PsahSl6URZFczI2ngT-lxfGTCkyCnNGdCVDvizbjDXzlT-7l5lEVq8CLRDA_XRb_d66v_YiQdR_IpLz8iANyg85uHuFeg05zq_w4XbsDY-Ono_WNhLuq_BYp0qHx0DydLcivpSzY1u2umCeR7JBVwYcczUXJLg',
    //         'Content-Type' => 'application/json'
    //     ])->post('https://adminapi.xinzhuvip.com/api/applet/wiki', [
    //         'page' => 1,
    //         'limit' => '1272'
    //     ]);

    //     // 处理响应
    //     $body = $response->getBody();
    //     $data = json_decode($body, true);
    //     $data = $data['data'];
    //     $insert = [];
    //     foreach ($data as $k => $v) {
    //         $arr = [];
    //         $arr['title'] = $v['title'];
    //         $arr['cover'] = $v['img'];
    //         $arr['type_id'] = $v['type'] ?? 0;
    //         $arr['content'] = $v['content'];
    //         $insert[] = $arr;
    //     }
    //     Post::insert($insert);
    //     return response()->json(['code' => 200, 'message' => '操作成功']);
    // }

    // public function index(Request $request)
    // {
    //     // 验证请求
    //     // 带请求头的请求
    //     $response = Http::withHeaders([
    //         'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiY2M1OGQzMzQ0YTUwZTU0ODE4ODA1MDcwYmJlMThmZjlhMThhY2M4MDgxYmM4OTc5NDJlNTUwZmNlNThjN2E2ZWI2OWUwNGI0OTI0MGVlNjciLCJpYXQiOjE3NTI4MjE3ODcuMjIyMTMsIm5iZiI6MTc1MjgyMTc4Ny4yMjIxMzksImV4cCI6MTc4NDM1Nzc4Ny4yMDkwODcsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.cse_j0C7dFqtpMa-Dcr2Oj8Z83kLskVigtJm7Dbj1yw9sXcf3Csb4xe9z2FVUprniXoACCtmZ0YhkkzGGJ9DJuljfrvsi2PaSk00ZWvx7VZCWmZUtmyXiIb1JdzLJ9yNK3ktQufJqdjNwDXsj6pmVYtWqfU3cxdZ5OgGPRdFtVzMJPj05HBwGHG-BYWyg0sepdavvGlx5eK5T-cgmtkbDMkcxD3xOkBVl4vvrCXNiEH3EyrwBvjGos9kM3jA876u2lUIQMcqPtsIRAZVrM755HPT-xSW9iKMR4rWZs5EQ8Wr7rYDAxqJx0QHXoR3GWUkiVE9mHjH-PXypsuKl9CNDL8QGxFKJIzTszJ6TsPpKllYocyqT4aahgBychjjzHSi2ilI_Fjs_HYYuQYh9cz_HqRjUkwWexxJDI1cWiw3SHa5GWfylEKUjTE8XQL028pTgcmJc6MrTaMxEffG33hyMqEO0ITgu7rGWsi3VdZGBo5zxF1VqnIxxA0BSws57QUZVPsrZVO4rKD23PsahSl6URZFczI2ngT-lxfGTCkyCnNGdCVDvizbjDXzlT-7l5lEVq8CLRDA_XRb_d66v_YiQdR_IpLz8iANyg85uHuFeg05zq_w4XbsDY-Ono_WNhLuq_BYp0qHx0DydLcivpSzY1u2umCeR7JBVwYcczUXJLg',
    //         'Content-Type' => 'application/json'
    //     ])->post('https://adminapi.xinzhuvip.com/api/applet/Node', [
    //         'page' => 1,
    //         'limit' => '603'
    //     ]);

    //     // 处理响应
    //     $body = $response->getBody();
    //     $data = json_decode($body, true);
    //     $data = $data['data'];
    //     $insert = [];
    //     foreach ($data as $k => $v) {
    //         $arr = [];
    //         $arr['title'] = $v['title'];
    //         $arr['cover'] = $v['img'];
    //         $arr['type_id'] = $v['type'];
    //         $arr['pan'] = $v['pan'];
    //         $arr['price'] = $v['price'];
    //         $arr['points'] = $v['integral'];
    //         $arr['clicks'] = $v['click'];
    //         $arr['content'] = $v['centent'];
    //         $insert[] = $arr;
    //     }
    //     Node::insert($insert);
    //     return response()->json(['code' => 200, 'message' => '操作成功']);
    // }

    // public function index(Request $request)
    // {
    //     // 验证请求
    //     // 带请求头的请求
    //     $response = Http::withHeaders([
    //         'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiY2M1OGQzMzQ0YTUwZTU0ODE4ODA1MDcwYmJlMThmZjlhMThhY2M4MDgxYmM4OTc5NDJlNTUwZmNlNThjN2E2ZWI2OWUwNGI0OTI0MGVlNjciLCJpYXQiOjE3NTI4MjE3ODcuMjIyMTMsIm5iZiI6MTc1MjgyMTc4Ny4yMjIxMzksImV4cCI6MTc4NDM1Nzc4Ny4yMDkwODcsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.cse_j0C7dFqtpMa-Dcr2Oj8Z83kLskVigtJm7Dbj1yw9sXcf3Csb4xe9z2FVUprniXoACCtmZ0YhkkzGGJ9DJuljfrvsi2PaSk00ZWvx7VZCWmZUtmyXiIb1JdzLJ9yNK3ktQufJqdjNwDXsj6pmVYtWqfU3cxdZ5OgGPRdFtVzMJPj05HBwGHG-BYWyg0sepdavvGlx5eK5T-cgmtkbDMkcxD3xOkBVl4vvrCXNiEH3EyrwBvjGos9kM3jA876u2lUIQMcqPtsIRAZVrM755HPT-xSW9iKMR4rWZs5EQ8Wr7rYDAxqJx0QHXoR3GWUkiVE9mHjH-PXypsuKl9CNDL8QGxFKJIzTszJ6TsPpKllYocyqT4aahgBychjjzHSi2ilI_Fjs_HYYuQYh9cz_HqRjUkwWexxJDI1cWiw3SHa5GWfylEKUjTE8XQL028pTgcmJc6MrTaMxEffG33hyMqEO0ITgu7rGWsi3VdZGBo5zxF1VqnIxxA0BSws57QUZVPsrZVO4rKD23PsahSl6URZFczI2ngT-lxfGTCkyCnNGdCVDvizbjDXzlT-7l5lEVq8CLRDA_XRb_d66v_YiQdR_IpLz8iANyg85uHuFeg05zq_w4XbsDY-Ono_WNhLuq_BYp0qHx0DydLcivpSzY1u2umCeR7JBVwYcczUXJLg',
    //         'Content-Type' => 'application/json'
    //     ])->post('https://adminapi.xinzhuvip.com/api/applet/course', [
    //         'page' => 1,
    //         'limit' => '37'
    //     ]);

    //     // 处理响应
    //     $body = $response->getBody();
    //     $data = json_decode($body, true);
    //     $data = $data['data'];
    //     $insert = [];
    //     foreach ($data as $k => $v) {
    //         $arr = [];
    //         $arr['title'] = $v['title'];
    //         $arr['cover'] = $v['cover'];
    //         $arr['original_price'] = $v['original_price'];
    //         $arr['price'] = $v['price'];
    //         $arr['brief'] = $v['brief'];
    //         $arr['content'] = $v['content'];
    //         $insert[] = $arr;
    //     }
    //     Course::insert($insert);
    //     return response()->json(['code' => 200, 'message' => '操作成功']);
    // }

    // public function index(Request $request)
    // {
    //     // 验证请求
    //     // 带请求头的请求
    //     $response = Http::withHeaders([
    //         'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiY2M1OGQzMzQ0YTUwZTU0ODE4ODA1MDcwYmJlMThmZjlhMThhY2M4MDgxYmM4OTc5NDJlNTUwZmNlNThjN2E2ZWI2OWUwNGI0OTI0MGVlNjciLCJpYXQiOjE3NTI4MjE3ODcuMjIyMTMsIm5iZiI6MTc1MjgyMTc4Ny4yMjIxMzksImV4cCI6MTc4NDM1Nzc4Ny4yMDkwODcsInN1YiI6IjEiLCJzY29wZXMiOltdfQ.cse_j0C7dFqtpMa-Dcr2Oj8Z83kLskVigtJm7Dbj1yw9sXcf3Csb4xe9z2FVUprniXoACCtmZ0YhkkzGGJ9DJuljfrvsi2PaSk00ZWvx7VZCWmZUtmyXiIb1JdzLJ9yNK3ktQufJqdjNwDXsj6pmVYtWqfU3cxdZ5OgGPRdFtVzMJPj05HBwGHG-BYWyg0sepdavvGlx5eK5T-cgmtkbDMkcxD3xOkBVl4vvrCXNiEH3EyrwBvjGos9kM3jA876u2lUIQMcqPtsIRAZVrM755HPT-xSW9iKMR4rWZs5EQ8Wr7rYDAxqJx0QHXoR3GWUkiVE9mHjH-PXypsuKl9CNDL8QGxFKJIzTszJ6TsPpKllYocyqT4aahgBychjjzHSi2ilI_Fjs_HYYuQYh9cz_HqRjUkwWexxJDI1cWiw3SHa5GWfylEKUjTE8XQL028pTgcmJc6MrTaMxEffG33hyMqEO0ITgu7rGWsi3VdZGBo5zxF1VqnIxxA0BSws57QUZVPsrZVO4rKD23PsahSl6URZFczI2ngT-lxfGTCkyCnNGdCVDvizbjDXzlT-7l5lEVq8CLRDA_XRb_d66v_YiQdR_IpLz8iANyg85uHuFeg05zq_w4XbsDY-Ono_WNhLuq_BYp0qHx0DydLcivpSzY1u2umCeR7JBVwYcczUXJLg',
    //         'Content-Type' => 'application/json'
    //     ])->post('https://adminapi.xinzhuvip.com/api/applet/product', [
    //         'page' => 1,
    //         'limit' => '20'
    //     ]);

    //     // 处理响应
    //     $body = $response->getBody();
    //     $data = json_decode($body, true);
    //     $data = $data['data'];
    //     $insert = [];
    //     foreach ($data as $k => $v) {
    //         $arr = [];
    //         $arr['title'] = $v['title'];
    //         $arr['cover'] = $v['banner'];
    //         $arr['price'] = $v['price'];
    //         $arr['brief'] = $v['brief'];
    //         $arr['content'] = $v['content'];
    //         $arr['contact'] = $v['concat'];
    //         $insert[] = $arr;
    //     }
    //     Product::insert($insert);
    //     return response()->json(['code' => 200, 'message' => '操作成功']);
    // }

}
